/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package realestatemarketclient;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author RedDevils
 */
public class rsTableModel extends AbstractTableModel{
    private Vector colHeaders;  //chua thong tin ten dung lam columnheader
    private Vector tbData;      //phan chua du lieu cua JTable
    
    public rsTableModel(ResultSet rsData) throws SQLException{
        ResultSetMetaData rsMeta = rsData.getMetaData(); //doc metadata cua resultset
        int count = rsMeta.getColumnCount(); //xac dinh so field trong ResultSet
        
        tbData = new Vector();
        colHeaders = new Vector(count);
        
        //lap so phan tu de lay ten cua tung cot trong bang
        for(int i=0; i<=count; i++){
            colHeaders.addElement(rsMeta.getColumnName(i));
        }
        
        //Lap tu record dau tien den cuoi ResultSet de lay du lieu add vao Table
        while(rsData.next()){
            //vector chua du lieu doc tu table
            Vector dataRow = new Vector(count);
            //lap theo so cot de lay thong tin dua vao dataRow
            for(int i = 1; i <= count; i++){
                dataRow.addElement(rsData.getObject(i));
            }
            tbData.addElement(dataRow);
        }
    }

    public int getRowCount() {
        return colHeaders.size();
    }

    public int getColumnCount() {
        return tbData.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Vector rowData = (Vector)(tbData.elementAt(rowIndex));
        return rowData.elementAt(columnIndex);
    }
}
